package com.seeyon.apps.relocationData.controller;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.ebs.rc.utils.RcUtils;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.affair.manager.AffairManager;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.ctp.common.template.manager.TemplateManager;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import org.apache.commons.logging.Log;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class RelocationDataController extends BaseController {

    private static final Log log = CtpLogFactory.getLog(RelocationDataController.class);
    private TemplateManager templateManager = (TemplateManager) AppContext.getBean("templateManager");
    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
    private static final String relocationCode = AppContext.getSystemProperty("relocationData.relocationCode");
    private AffairManager affairManager = (AffairManager) AppContext.getBean("affairManager");
    private final OrgManager orgManager =(OrgManager) AppContext.getBean("orgManager");

    public ModelAndView getSectionList1(HttpServletRequest request, HttpServletResponse response) {
        String sectionCount = request.getParameter("sectionCount");
        User currentUser = AppContext.getCurrentUser();
        Long id = currentUser.getId();
        ModelAndView modelAndView = new ModelAndView("/plugin/relocationData/relocationData");
        try {
            JSONObject jsonObject = RcUtils.personalScheduleRoles(id);
            Integer grade = jsonObject.getInteger("grade");
            if (grade!=1&&grade!=2){
                modelAndView.addObject("hasLeader",false);
            }else {
                modelAndView.addObject("hasLeader",true);
            }
            log.info("获取人员职级成功数据:"+jsonObject);
        }catch (Exception e){
            log.error("获取人员职级失败",e);
        }
        return modelAndView;

    }

    public ModelAndView getPersonSectionList(HttpServletRequest request, HttpServletResponse response) {
        String sectionCount = request.getParameter("sectionCount");
        return new ModelAndView("/plugin/relocationData/personRelocationData");
    }


    public void checkRelocation(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=UTF-8");
        String templateId = request.getParameter("templateId");
        String affairId = request.getParameter("affairId");
        JSONObject returnResult = new JSONObject();
        try {
            PrintWriter writer = response.getWriter();
            CtpTemplate template = this.templateManager.getCtpTemplate(Long.parseLong(templateId));
            if (template == null) {
                returnResult.put("data", false);
            } else {
                FormBean formBean = this.cap4FormManager.getFormByFormCode(relocationCode);
                Long id = formBean.getId();
                Long formAppId = template.getFormAppId();
                if (id.equals(formAppId)){
                    returnResult.put("data", true);
                    if (affairId==null){
                        User currentUser = AppContext.getCurrentUser();
                        Long userId = currentUser.getId();
                        String name = currentUser.getName();
                        returnResult.put("userId",userId);
                        returnResult.put("userName",name);
                    }else {
                        CtpAffair ctpAffair = affairManager.get(Long.parseLong(affairId));
                        Long formRecordid = ctpAffair.getFormRecordid();
                        FormFieldBean nameBean = formBean.getFieldBeanByDisplay("Name");
                        FormDataMasterBean masterData = cap4FormManager.getDataMasterBeanById(formRecordid, formBean, null);
                        Long memberId = Long.parseLong(String.valueOf(masterData.getFieldValue(nameBean.getName())));
                        returnResult.put("userId",memberId);
                        V3xOrgMember member = orgManager.getMemberById(memberId);
                        String name = member.getName();
                        returnResult.put("userName",name);
                    }
                }else {
                    returnResult.put("data", false);
                }
            }
            writer.write(returnResult.toString());
        } catch (Exception e) {
            logger.error("判断是否动迁申请失败",e);
        }
    }

    public ModelAndView showPersonRelocation(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView("/plugin/relocationData/personRelocationButton");
        return mav;
    }

}
